/**
 * Created by kevin on 2017/01/08.
 *
 * UI层
 * 音乐icon
 */


var eosi = window.eosi || {};
this.eosi.website = this.eosi.website || {};

(function (window) {
    // the application


    function ContentLayer() {

        this.Container_constructor();

        this.init();
        //console.log(this.father.targetWidth);

    }
    //PopupLayer.TYPE = "people";

    var p = createjs.extend(ContentLayer, createjs.Container);

    p.className = "ContentLayer";
    //加载器
    p.loader;
    //配置
    p.pageConfigs=[];
    //页面
    p.pages = [];
    //资源是否加载完毕
    p.resIsLoaded = false;
    //上一个页面
    p.lastPage;

    // public methods:
    p.init = function () {
        //this.loadRes();
        //console.log(this.className);
        this.pageConfigs = pages;
        for(var i = 0;i<this.pageConfigs.length;i++){
            //console.log(this.pageConfigs[i].name ,cur_page);
            if(this.pageConfigs[i].name == cur_page){
                //console.log(cur_page,i);
                BaseLoader_list = new eosi.website.BaseLoader();
                //实例当前页面
                // this.createPage(i,true);
                //this.lastPage = curPage;
            }
        }
    }


    /*
     * 加载资源
     * */
   /* p.loadRes = function(){


        this.loader = new createjs.LoadQueue(true);
        //var loadItem = {src: "../static/ContentLayerManifest.json?v="+version, callback: "loadContentMediaGrid", type: "jsonp"}
        //this.loader = new createjs.LoadItem(loadItem);
        //this.loader.on("complete", this.loadComplete, this);
        this.loader.on("fileload", this.onLoaded, this);
        //this.loader.on("error", this.onError, this);

        //this.loader.load(loadItem)
        this.loader.loadManifest({src: BASE_PATH + "static/ContentLayerManifest.json?v="+version, callback: "loadContentMediaGrid", type: "jsonp"}, true, "");
        //this.loader.load();
    }
    p.onError = function(evt){
        console.log(BASE_PATH + "static/ContentLayerManifest.json?v=")
        console.log(evt)
    }*/
    /*
    * 资源加载
    * */
    /*p.onLoaded = function (evt){
        var result = evt.result;
        this.pageConfigs=result;
        //console.log(this.pages)

        for(var i = 0;i<this.pageConfigs.length;i++){
            console.log(this.pageConfigs[i].name ,cur_page);
            if(this.pageConfigs[i].name == cur_page){
                //console.log(cur_page,i);
                //实例当前页面
                this.createPage(i,true);
                //this.lastPage = curPage;
            }
        }
        //console.log(this.pageConfigs)

    }*/


    /*
    * 实例page
    * index:页面索引，
    * isCur:是否是当前页
    * */
    p.createPage = function(index,isCur){
        var page = this.getChildByName(this.pageConfigs[index].id);

        //console.log(page)
        if(page == null){
            switch (this.pageConfigs[index].p_type.type){
                case "default":
                    // page = new eosi.website.BasePage(this.pageConfigs[index]);
                    page = new eosi.website.BasePage_globalload(this.pageConfigs[index]);
                    page.name = this.pageConfigs[index].id;
                    //console.log(page.name)
                    page.pageIndex = index;
                    page.loader=BaseLoader_list;
                    //this.lastPage = page;
                    this.addChild(page);
                    break;
                // case "fullScreenVideo":
                //     page = new eosi.website.VideoPage(this.pageConfigs[index],index);
                //     page.name = this.pageConfigs[index].id;
                //     //console.log(page.name)
                //     page.pageIndex = index;
                //     // page.loader=BaseLoader_list
                //     //this.lastPage = page;
                //     this.addChild(page);
                //     break;
                /*case "class":
                    var tmpClass = eval(this.pageConfigs[index].p_type.className)
                    page = new tmpClass(this.pageConfigs[index]);
                    page.name = this.pageConfigs[index].id;
                    //console.log(page.name)
                    page.pageIndex = index;
                    //this.lastPage = page;
                    this.addChild(page);
                    break;*/

            }
        }else{
            if(!page.isDraw){
                page.visible = true;
                if(page.resIsLoaded){
                    page.drawPage();
                }

            }else{
                switch (this.pageConfigs[index].reload_type){
                    case "animation"://动画重置
                        page.visible = true;
                        page.resetAnimation();
                        break;
                    case "none"://不加载，不重置动画
                        page.visible = true;
                        break;
                    default :
                        page.visible = true;
                        break;

                }
            }

            //page.name = this.pageConfigs[index].id;
            //this.lastPage = page;

        }
        //是当前页面
        if(isCur){
            this.lastPage = page;
            //console.log(this.lastPage,page.name)
            window.location =VIRTURL_PATH + "#/"+this.pageConfigs[index].id;
        }
        //console.log(this.lastPage.pageIndex)
        if(UILayer){
            UILayer.setIconStatus(this.pageConfigs[this.lastPage.pageIndex].button_on);
        }


        // console.log(page)
        //return page;
    }

    /*
    * 删除老页面
    * */
    p.removeBeforePage = function(){
        if(this.lastPage){
            switch (this.pageConfigs[this.lastPage.pageIndex].reload_type){
                case "totaly"://全部重置，包括重新加载
                    this.removeAllChildren(this.lastPage);
                    this.lastPage = null;
                    break;
                case "animation"://动画重置
                    this.lastPage.visible = false;
                    break;
                case "none"://不加载，不重置动画
                    this.lastPage.visible = false;
                    break;
                default :
                    this.lastPage.visible = false;
                    break;

            }

        }

    }

    /*
    * jump to
    * */
    p.jumpTo = function(page){
        //console.log(page);
        this.removeBeforePage();
        for(var i = 0;i<this.pageConfigs.length;i++){
            if(this.pageConfigs[i].id == page){
                this.createPage(i,true);
                break;
            }
        }

    }

    /*
    * preload
    * pageIndex：刚刚加载完毕的index
    * */
    p.preload = function(pageIndex){
        // console.log(pageIndex,this.pageConfigs[pageIndex]);
        this.pageConfigs[pageIndex].resIsLoaded = true;
        //console.log(this.pageConfigs.length)
        for(var i =0 ;i<this.pageConfigs.length;i++){

            if(this.pageConfigs[i].resIsLoaded==undefined){
                //console.log(i);
                //console.log(this.pageConfigs[i].resIsLoaded)
                this.createPage(i);
            }
        }
    }
    p.tick = function(evt){
        if(this.lastPage){
            this.lastPage.tick(evt);
        }
        //console.log(this.lastPage);
    }



    // add MyApp to myNameSpace
    window.eosi.website.ContentLayer = createjs.promote(ContentLayer, "Container");
//}());
}(window));





